/*-
 * Copyright (c) 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      DocumentEntryIterator.hpp
 *
 * $FTS$
 */
#ifndef _DOCUMENT_ENTRY_ITERATOR_HPP__
#define _DOCUMENT_ENTRY_ITERATOR_HPP__ 1

/**
  @file DocumentEntryIterator.hpp
  @brief DocumentEntry iterator
*/
#include <FTS.hpp>

namespace FTSS // FullTextSearchSystem
{

/**
  @class DocumentEntryIteratorBase DocumentEntryIterator.hpp <DocumentEntryIterator.hpp>
  @brief DocumentEntry base iterator template
*/
template <typename T> class DocumentEntryIteratorBase
{
public:
	/** Document Entry                */
	void          * vData;
	/** Current offset                */
	Size_t          iOffset;
	/** Current position              */
	T             * pCurrentEntry;
public:
	/**
	  @brief Constructor
	  @param vIData - data
	  @param iIOffset - data offset
	*/
	inline DocumentEntryIteratorBase(void * vIData, const Size_t & iIOffset): vData(vIData), iOffset(iIOffset)
	{
		pCurrentEntry = (T *)vData;
	}

	/**
	  @brief Operator *
	  @return reference to Document Entry
	*/
	inline T & operator*() throw() { return *pCurrentEntry; }

	/**
	  @brief Operator ->
	  @return reference to Document Entry
	*/
	inline T & operator->() throw() { return *pCurrentEntry; }

	/**
	  @brief Pre increment operator
	  @return reference to self
	*/
	inline DocumentEntryIteratorBase<T> & operator++() throw()
	{
		iOffset += pCurrentEntry -> size;
		pCurrentEntry = (T *)((CHAR_8 *)pCurrentEntry + pCurrentEntry -> size);

	return *this;
	}

	/**
	  @brief Pre increment operator
	  @return reference to self
	*/
	inline Size_t operator-(const DocumentEntryIteratorBase<T> & x) throw()
	{
		return x.iOffset - iOffset;
	}

	/**
	  @brief Post increment operator
	  @return reference to self
	*/
	inline DocumentEntryIteratorBase<T> operator++(int) throw()
	{
		DocumentEntryIteratorBase<T> oTMP = *this;

		iOffset += pCurrentEntry -> size;
		pCurrentEntry = (T *)((CHAR_8 *)pCurrentEntry + pCurrentEntry -> size);

	return oTMP;
	}

	/**
	  @brief Compare two iterators
	  @return true if iterators are equal
	*/
	inline bool operator==(const DocumentEntryIteratorBase<T> & x) const throw() { return ((iOffset == x.iOffset) && (vData == x.vData)); }

	/**
	  @brief Compare two iterators
	  @return true if iterators are NOT equal
	*/
	inline bool operator!=(const DocumentEntryIteratorBase<T> & x) const throw() { return !((iOffset == x.iOffset) && (vData == x.vData)); }

	/**
	  @brief Get data
	  @return pointer to current data
	*/
	inline T * operator()() throw() { return pCurrentEntry; }

};

/**
  @var DocumentEntryIteratorBase<DocumentEntry>         DocumentEntryIterator
  @brief DocumentEntry iterator
*/
typedef DocumentEntryIteratorBase<DocumentEntry>         DocumentEntryIterator;

/**
  @var DocumentEntryIteratorBase<const DocumentEntry>   DocumentEntryConstIterator
  @brief DocumentEntry constant iterator
*/
typedef DocumentEntryIteratorBase<const DocumentEntry>   DocumentEntryConstIterator;

} // namespace FTSS
#endif // _DOCUMENT_ENTRY_ITERATOR_HPP__
// End.
